var t = require("../../../@babel/runtime/helpers/interopRequireDefault").default, e = t(require("@vant/weapp/dialog/dialog.js")), a = t(require("../../../api/api_shareCard")), i = (t(require("../../../api/auth.js")), 
t(require("../../../utils/Tips"))), o = require("../../../config/Links"), n = t(require("../../../behaviors/globalData")), r = require("../../../utils/util"), s = t(require("../../../behaviors/aucMiniAuth")), l = getApp();

l.eventBus;

Component({
    behaviors: [ n.default, s.default ],
    properties: {},
    data: {
        actionEndFlag: !1,
        phone: l.globalData.aucPhone,
        currentTableId: "",
        currentInvitedSerialNumber: "",
        currentIndex: 0,
        showPage: !1,
        showInput: 2,
        showPopBox: !1,
        qqNumber: "",
        currentMonth: (0, r.getYearAndMonth)() ? (0, r.getYearAndMonth)() : "",
        shareCardImageBaseUrl: o.imageBaseUrl.shareCard,
        beiSharedUser: {
            phone: "",
            headimg: o.imageBaseUrl.shareCard + "headimg_default.png"
        },
        giftlist: [],
        defaultTempList: [ {
            logo: 0,
            btn: 1,
            time: ""
        }, {
            logo: -1,
            btn: 0
        }, {
            logo: -1,
            btn: 0
        } ],
        logoList: [],
        chooseLogo: 1,
        logo_default_grey: o.imageBaseUrl.shareCard + "logo_default_grey.png",
        logo_default: o.imageBaseUrl.shareCard + "logo_default.png",
        logo_tx: "https://txwk.10010.com/static/gift_icon/icon_QQVideo.png",
        logo_qq: "https://txwk.10010.com/static/gift_icon/icon_qqSuperVip.png",
        logo_lv: "https://txwk.10010.com/static/gift_icon/icon_QQMusic.png",
        btn_receive: o.imageBaseUrl.shareCard + "btn_receive_v2.png",
        btn_received: o.imageBaseUrl.shareCard + "btn_received.png",
        btn_overtime: o.imageBaseUrl.shareCard + "btn_overtime.png",
        btn_receive_grey: o.imageBaseUrl.shareCard + "btn_receive_grey.png"
    },
    methods: {
        init: function() {
            var t = this;
            return new Promise(function(e, a) {
                var i = t;
                i.getLogoList().then(function() {
                    return Promise.all([ i.getQQ(), i.getInviteOrder() ]);
                }).then(function() {
                    wx.hideLoading(), e();
                }).catch(function(t) {
                    wx.hideLoading(), a();
                });
            });
        },
        getQQ: function() {
            var t = this;
            wx.showLoading({
                title: "加载中"
            });
            var i = this;
            return this.data.globalData && this.data.globalData.globalData_qqNumber ? (this.setData({
                qqNumber: this.data.globalData.globalData_qqNumber
            }), !0) : (a.default.getQQ().then(function(a) {
                wx.hideLoading();
                var o = a.code, n = a.result, r = a.msg;
                "0" === o ? n && n[0] ? (t.setData({
                    qqNumber: n[n.length - 1],
                    showInput: 2
                }), t.setGlobalData({
                    globalData_qqNumber: i.data.qqNumber
                })) : t.setData({
                    showInput: 1
                }) : e.default.alert({
                    context: i,
                    message: r
                }), wx.hideLoading();
            }).catch(function(t) {
                e.default.alert({
                    context: i,
                    message: t
                }), wx.hideLoading();
            }), !0);
        },
        getInviteOrder: function() {
            wx.showLoading();
            var t = this;
            return t.setData({
                giftlist: [],
                showPage: !1
            }), new Promise(function(e, o) {
                a.default.getInviteOrder().then(function(a) {
                    console.log("获取分享记录成功", a);
                    var n = a.code, r = a.result, s = a.msg;
                    if (t.setData({
                        showPage: !0
                    }), "0" === n) if (r && r.length >= 0) {
                        var l = [];
                        r = r.filter(function(t) {
                            return 2 === t.businessStatus || 3 === t.businessStatus;
                        });
                        for (var u = function(a) {
                            var i = r[a];
                            t.getGiftListItem(i.tableId, i.invitedSerialNumber).then(function(t) {
                                l[a] = t;
                            }).then(function() {
                                t.setData({
                                    giftlist: l,
                                    showPage: !0
                                }), e();
                            });
                        }, c = 0; c < r.length; c++) u(c);
                    } else o(), i.default.toast("无分享记录"); else wx.showModal({
                        content: s,
                        showCancel: !1
                    }), o();
                }).catch(function(t) {
                    console.log("获取分享记录失败", t), o(), wx.hideLoading();
                });
            });
        },
        getGiftListItem: function(t, i, o) {
            var n = this;
            return new Promise(function(o, r) {
                if (!t) return !0;
                a.default.getGiftListItem({
                    tableId: t
                }).then(function(a) {
                    console.log("获取订单详情", a);
                    var r = a.code, s = a.result, l = a.msg;
                    if ("0" === r && s) return n.dealResult(s, i, t, function(t) {
                        o(t);
                    });
                    e.default.alert({
                        context: n,
                        message: l
                    });
                }).catch(function(t) {
                    r(), wx.hideLoading();
                });
            });
        },
        dealResult: function(t, e, a, i) {
            var o = this, n = [];
            t.forEach(function(t, e) {
                var i = {};
                switch (t.giftCode) {
                  case o.data.logoList[0].giftCode:
                    i.logo = 1;
                    break;

                  case o.data.logoList[1].giftCode:
                    i.logo = 2;
                    break;

                  case o.data.logoList[2].giftCode:
                    i.logo = 3;
                    break;

                  default:
                    t.isExpire || 11 !== t.status || Number(t.effectiveDate) !== Number(o.data.currentMonth) ? i.logo = -1 : i.logo = 0;
                }
                t.isExpire ? i.btn = 3 : 10 === t.status ? i.btn = 2 : 11 !== t.status && null !== t.status || (Number(t.effectiveDate) === Number(o.data.currentMonth) ? i.btn = 1 : Number(t.effectiveDate) > Number(o.data.currentMonth) ? (i.btn = 0, 
                i.time = Number(t.effectiveDate.slice(-2))) : i.btn = 3), i.giftCode = t.giftCode, 
                i.tableId = a, n.push(i);
            }), i({
                tableId: a,
                invitedSerialNumber: e,
                tempList: n
            });
        },
        clickChangeTime: function() {
            this.setData({
                showInput: 1
            });
        },
        getSearchInput: function(t) {
            this.setData({
                qqNumber: t.detail.value
            });
        },
        clickConfirmQQ: function() {
            if (!this.data.qqNumber) return i.default.toast("请输入QQ号"), !0;
            return this.setGlobalData({
                globalData_qqNumber: this.data.qqNumber
            }), this.setData({
                showInput: 2
            }), !0;
        },
        clickReceive: function(t) {
            console.log("clickReceive", t), this.setData({
                currentTableId: t.currentTarget.dataset.tableid,
                currentInvitedSerialNumber: t.currentTarget.dataset.invitedserialnumber,
                currentIndex: t.currentTarget.dataset.index
            }), "0" === t.currentTarget.dataset.giftcode ? this.data.globalData.globalData_qqNumber ? this.showPopBox() : i.default.toast("请输入QQ号") : this.receive2();
        },
        showPopBox: function() {
            this.setData({
                chooseLogo: 1
            }), this.setData({
                showPopBox: !0
            });
        },
        onClose: function() {
            this.setData({
                showPopBox: !1
            });
        },
        chooseLogo: function(t) {
            this.setData({
                chooseLogo: t.currentTarget.dataset.index
            });
        },
        receive: function() {
            var t = this, e = this, o = {
                giftRulecode: this.data.logoList[this.data.chooseLogo].giftRuleCode,
                account: this.data.globalData.globalData_qqNumber,
                tableId: this.data.currentTableId
            };
            o.giftRulecode && o.account && o.tableId ? (wx.showLoading({
                title: "领取中"
            }), a.default.receive(o).then(function(a) {
                console.log("领取成功", a), l.kta.clickStat("474b3bf4a7dc4b47b7d3f5b5153c9f26");
                var o = a.code, n = a.msg;
                "0" === o ? (i.default.toast("领取成功"), e.onClose(), e.getGiftListItem(t.data.currentTableId, t.data.currentInvitedSerialNumber).then(function(a) {
                    var i = t.data.giftlist;
                    i[t.data.currentIndex] = a, e.setData({
                        giftlist: i
                    });
                })) : i.default.toast(n), wx.hideLoading();
            }).catch(function(t) {
                console.log("领取失败", t), i.default.toast("领取失败"), wx.hideLoading();
            })) : i.default.toast("无礼品或qq号或tableid");
        },
        receive2: function() {
            var t = this, e = this;
            if (this.data.globalData.globalData_qqNumber && this.data.currentTableId) {
                var o = {
                    account: this.data.globalData.globalData_qqNumber,
                    tableId: this.data.currentTableId
                };
                wx.showLoading({
                    title: "领取中"
                }), a.default.receive2(o).then(function(a) {
                    console.log("领取成功", a), l.kta.clickStat("474b3bf4a7dc4b47b7d3f5b5153c9f26");
                    var o = a.code, n = a.msg;
                    "0" === o ? (i.default.toast("领取成功"), e.onClose(), e.getGiftListItem(e.data.currentTableId, e.data.currentInvitedSerialNumber).then(function(a) {
                        var i = e.data.giftlist;
                        i[t.data.currentIndex] = a, e.setData({
                            giftlist: i
                        });
                    })) : i.default.toast(n), wx.hideLoading();
                }).catch(function(t) {
                    console.log("领取失败", t), i.default.toast("领取失败"), wx.hideLoading();
                });
            } else i.default.toast("请输入QQ号");
        },
        getLogoList: function() {
            var t = this;
            wx.showLoading();
            var i = this;
            return new Promise(function(o, n) {
                a.default.getLogoList().then(function(a) {
                    l.globalData.aucPhone && i.setData({
                        phone: l.globalData.aucPhone
                    });
                    var r = a.code, s = a.result, u = a.msg;
                    "0" === r && s && s.list ? (t.setData({
                        logoList: s.list
                    }), o()) : (e.default.alert({
                        context: i,
                        message: u
                    }), n()), wx.hideLoading();
                }).catch(function(t) {
                    e.default.alert({
                        context: i,
                        message: "请求失败"
                    }), wx.hideLoading(), n();
                });
            });
        },
        showBindPopBox: function() {
            e.default.confirm({
                context: this,
                confirmButtonOpenType: "getPhoneNumber",
                bindGetphonenumber: "bindgetphonenumber_auc",
                confirmButtonText: "绑定号码",
                cancelButtonText: "取消",
                richTextMessage: !0,
                richText: [ {
                    name: "div",
                    children: [ {
                        type: "text",
                        text: "未绑定手机号码，无法领取礼品"
                    } ]
                } ]
            });
        },
        afterBindgetphonenumber: function(t, a) {
            var o = this, n = t.code, r = t.data;
            "0" === n && r && r.phone ? (l.globalData.aucPhone = r.phone, o.setData({
                phone: r.phone
            }), i.default.toast("绑定成功，可领取"), l.globalData.aucMiniAuth.sid = "", o.init().then(function() {
                o.clickReceive(a);
            })) : e.default.alert({
                context: o,
                message: "解密失败，请重试"
            });
        }
    },
    created: function() {},
    attached: function() {
        l.globalData.loginCode = !1, l.globalData.hasAuth = !1, l.globalData.aucMiniAuth.sid = "";
        l.globalData.actionEndFlag && (this.setData({
            actionEndFlag: !0
        }), wx.hideShareMenu()), this.init();
    },
    detached: function() {
        this.refreshSub && this.refreshSub.off();
    }
});